Skip to content

Wake the event loop on SIGINT so async cells interrupt promptly - #1614

Open
vivekghodekar001 wants to merge 2 commits into
ipython:mainfrom
vivekghodekar001:fix/sigint-wakeup-fd
Open

vivekghodekar001 wants to merge 2 commits into
ipython:mainfrom
vivekghodekar001:fix/sigint-wakeup-fd

Conversation

@vivekghodekar001

Copy link
Copy Markdown

Fixes #881

While an async cell runs, _cancel_on_sigint installs a Python-level SIGINT handler. CPython only runs Python signal handlers when the main thread executes bytecode, so when the loop is parked in its selector (for example awaiting asyncio.sleep), the handler does not run until something else wakes the loop. Interrupting the cell therefore only takes effect when the sleep ends.

This registers a wakeup fd for the duration of _cancel_on_sigint, the same mechanism loop.add_signal_handler() uses, so SIGINT wakes the selector and the cancellation lands promptly. An existing wakeup fd is left untouched, and the fd and previous handler are restored when the context exits.

Adds a regression test that sends SIGINT from a timer thread while a future sleeps and asserts cancellation lands promptly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot interrupt top-level coroutine

1 participant